package org.xmh.util;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FileFilter implements javax.servlet.Filter {

	private HttpServletRequest request;
	private HttpServletResponse response;

	private String jsp = "jsp";
	private String error = "/error.html";
	private String root = "";
	private String module = "";

	public void destroy() {
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest,
	 * javax.servlet.ServletResponse, javax.servlet.FilterChain)
	 */
	public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
		request = (HttpServletRequest) req;
		response = (HttpServletResponse) res;

	/*	String servletPaht = request.getServletPath();
		String currentUrl = request.getRequestURL().toString();
		int idx = currentUrl.lastIndexOf(servletPaht);
		String url = currentUrl.substring(0, idx > 0 ? idx : currentUrl.length());
		System.out.println("请求URL:" + request.getRequestURL());
		// 当请求的URL包含点字符串
		if (servletPaht.indexOf(".") != -1) {
			// 是否是配置的阻止访问的文件后缀
			boolean isJspFile = isJspFile();
			// 如果不是,则直接转发到文件 如css,js
			if (!isJspFile) {
				RequestDispatcher rd = request.getRequestDispatcher(servletPaht);
				rd.forward(request, response);
				// 请求了阻止访问的文件
			} else {
				// 将重定向到错误提示页
				response.sendRedirect(url + this.error);
			}
			return;
			// 请求的URL不包含点字符串
		} else {
			boolean isRequestJsp = isRequestJsp();
			if (isRequestJsp) {
				RequestDispatcher rd = request.getRequestDispatcher(this.root + servletPaht + "." + this.jsp);
				rd.forward(request, response);
				return;
			}
		}*/
		chain.doFilter(request, response);
	}

	/**
	 * 是否是jsp文件
	 * 
	 * @return
	 * @throws IOException
	 */
	private boolean isJspFile(){
		String servletPaht = request.getServletPath();
		if (!"".equals(jsp) && servletPaht.endsWith(jsp)) {
			return true;
		}
		return false;
	}

	/**
	 * @return
	 */
	private boolean isRequestJsp() {
		String servletPaht = request.getServletPath();
		String[] modules = this.module.split(",");
		for (String m : modules) {
			if (!"".equals(m)) {
				m = m.replaceAll("\\." + jsp + "$", "");
				if (servletPaht.indexOf(m) == 0) {
					return true;
				}
			}
		}
		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
	 */
	public void init(FilterConfig filterConfig) throws ServletException {
		// 获得阻止请求文件的请求时,展示用户提示页面路径
		this.error = filterConfig.getInitParameter("error");
		// jsp页面存放的根目录
		this.root = filterConfig.getInitParameter("root");
		// 标识可以不显示请求文件后缀可以访问的文件夹
		this.module = filterConfig.getInitParameter("module");
	}

}
